﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using BLL.BaseDictionaryService;
using Model;
using BLL.EarningService;

namespace FamilyManager
{
    public partial class EarningRecordsQueryForm : Form
    {
        private EarningRecordsQueryForm()
        {
            InitializeComponent();
        }

        private static EarningRecordsQueryForm instance;

        public static EarningRecordsQueryForm CreateInstance()
        {
            if (instance == null)
            {
                instance = new EarningRecordsQueryForm();
            }
            return instance;
        }

        private void BindcbUsers()
        {
            QueryUsersTransaction command = new QueryUsersTransaction();
            command.Execute();
            T_UserObject user = new T_UserObject();
            user.Db_OID = 0;
            user.Db_NickName = "";
            command.Users.Insert(0, user);
            cbUsers.DataSource = command.Users;
            cbUsers.DisplayMember = "Db_NickName";
            cbUsers.ValueMember = "Db_OID";
        }

        private void BindcbEarningCategory()
        {
            QueryEarningCategoryTransaction objQueryEarningCategoryTransaction = new QueryEarningCategoryTransaction();
            objQueryEarningCategoryTransaction.Execute();
            D_EarningCategoryObject category = new D_EarningCategoryObject { Db_OID = 0, Db_EarningCategory = "" };
            objQueryEarningCategoryTransaction.ResultList.Insert(0, category);
            cbCategory.DataSource = objQueryEarningCategoryTransaction.ResultList;
            cbCategory.DisplayMember = "db_EarningCategory";
            cbCategory.ValueMember = "Db_OID";
        }



        private void EarningRecordsQueryForm_Load(object sender, EventArgs e)
        {
            BindcbEarningCategory();
            BindcbUsers();
            InitDateTime();
        }

        private void InitDateTime()
        {
            DateTime dtBeginDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime dtEndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, ToolBox.GetEndDay(DateTime.Now.Year, DateTime.Now.Month));
            dtpBeginDate.Value = dtBeginDate;
            dtpEndDate.Value = dtEndDate;
        }

        /// <summary>
        /// 切换到上个月
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnPreviousMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnPreviousMonth.Checked)
            {
                DateTime dt = DateTime.Now.AddMonths(-1);
                DateTime dtBeginDate = new DateTime(dt.Year, dt.Month, 1);
                DateTime dtEndDate = new DateTime(dt.Year, dt.Month, ToolBox.GetEndDay(dt.Year, dt.Month));
                dtpBeginDate.Value = dtBeginDate;
                dtpEndDate.Value = dtEndDate;
            }
        }

        /// <summary>
        /// 切换到上季度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnPreviousQuarter_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnPreviousQuarter.Checked)
            {
                QuarterOfYear quarter = ToolBox.GetPreviousQuarter(DateTime.Now);
                dtpBeginDate.Value = quarter.BeginDate;
                dtpEndDate.Value = quarter.EndDate;
            }
        }

        /// <summary>
        /// 切换到上半年
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnPreviousHalfYear_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnPreviousHalfYear.Checked)
            {
                HalfYearObject halfYear = ToolBox.GetPreviousHalfYear(DateTime.Now);
                dtpBeginDate.Value = halfYear.BeginDate;
                dtpEndDate.Value = halfYear.EndDate;
            }
        }

       /// <summary>
       /// 切换到上一年
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void rbtnrbtnPreviousYear_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnrbtnPreviousYear.Checked)
            {
                DateTime dt = DateTime.Now.AddYears(-1);
                DateTime dtBeginDate = new DateTime(dt.Year, 1, 1);
                DateTime dtEndDate = new DateTime(dt.Year, 12, 31);
                dtpBeginDate.Value = dtBeginDate;
                dtpEndDate.Value = dtEndDate;
            }
        }

        private T_EarningRecordsObject CreateCondition()
        {
            T_EarningRecordsObject result = new T_EarningRecordsObject();
            result.BeginDate = dtpBeginDate.Value;
            result.EndDate = dtpEndDate.Value;
            result.Db_EarningObject = (int)cbUsers.SelectedValue;
            result.Db_EarningCategory = (int)cbCategory.SelectedValue;
            return result;
        }

        private void BingvEarningList()
        {
            gvEarningList.AutoGenerateColumns = false;
            gvEarningList.Columns.Clear();

            NomalQueryEarningRecordTransaction objNomalQueryEarningRecordTransaction = new NomalQueryEarningRecordTransaction(CreateCondition());
            IList<T_EarningRecordsObject> list = objNomalQueryEarningRecordTransaction.Query();
            gvEarningList.DataSource = list;
            DataGridViewTextBoxColumn dcDate = new DataGridViewTextBoxColumn();
            dcDate.DataPropertyName = "Db_EarningDate";
            dcDate.HeaderText = "收入日期";
            gvEarningList.Columns.Add(dcDate);

            DataGridViewTextBoxColumn dcSum = new DataGridViewTextBoxColumn();
            dcSum.DataPropertyName = "Db_EarningSum";
            dcSum.HeaderText = "收入金额";
            gvEarningList.Columns.Add(dcSum);

            DataGridViewTextBoxColumn dcCategory = new DataGridViewTextBoxColumn();
            dcCategory.DataPropertyName = "EarningCategory";
            dcCategory.HeaderText = "收入类别";
            gvEarningList.Columns.Add(dcCategory);

            DataGridViewTextBoxColumn dcUser = new DataGridViewTextBoxColumn();
            dcUser.DataPropertyName = "EarningUser";
            dcUser.HeaderText = "收入人";
            gvEarningList.Columns.Add(dcUser);

            DataGridViewTextBoxColumn dcMemo = new DataGridViewTextBoxColumn();
            dcMemo.DataPropertyName = "Db_EarningMemo";
            dcMemo.HeaderText = "备注";
            gvEarningList.Columns.Add(dcMemo);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            BingvEarningList();
        }

        /// <summary>
        /// 当前月份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnCurrentMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCurrentMonth.Checked)
            {
                InitDateTime();
            }
        }
    }
}
